package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.dto.ContractPersonnelDTO;
import com.kaizeli.website.pojo.entity.ContractPersonnelDO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.ContractPersonnelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 合同人员管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/contract-personnel")
@Tag(name = "合同人员管理", description = "合同人员相关接口")
public class ContractPersonnelController {
    
    @Autowired
    private ContractPersonnelService contractPersonnelService;
    
    @Operation(summary = "根据项目编号获取合同人员列表", description = "根据项目编号获取该项目的合同人员列表")
    @GetMapping("/project/{projectCode}")
    public Result<List<ContractPersonnelDO>> getPersonnelByProjectCode(
            @Parameter(description = "项目编号", required = true, example = "PROJ001")
            @PathVariable String projectCode) {
        log.info("根据项目编号获取合同人员列表，项目编号：{}", projectCode);
        return contractPersonnelService.getPersonnelByProjectCode(projectCode);
    }
    
    @Operation(summary = "创建合同人员", description = "创建新的合同人员记录")
    @PostMapping
    public Result<ContractPersonnelDO> createContractPersonnel(
            @Parameter(description = "合同人员信息", required = true)
            @RequestBody ContractPersonnelDO contractPersonnel) {
        log.info("创建合同人员，人员姓名：{}", contractPersonnel.getPersonnelName());
        return contractPersonnelService.createContractPersonnel(contractPersonnel);
    }
    
    @Operation(summary = "批量创建合同人员", description = "批量创建合同人员记录")
    @PostMapping("/batch")
    public Result<List<ContractPersonnelDO>> batchCreateContractPersonnel(
            @Parameter(description = "合同人员列表", required = true)
            @RequestBody List<ContractPersonnelDTO> contractPersonnelList) {
        log.info("批量创建合同人员，数量：{}", contractPersonnelList.size());
        // 将DTO转换为DO
        List<ContractPersonnelDO> doList = contractPersonnelList.stream()
                .map(dto -> {
                    ContractPersonnelDO personnel = new ContractPersonnelDO();
                    personnel.setProjectCode(dto.getProjectCode());
                    personnel.setContractNo(dto.getContractNo());
                    personnel.setPersonnelName(dto.getPersonnelName());
                    personnel.setPersonnelType(dto.getPersonnelType());
                    personnel.setPosition(dto.getPosition());
                    personnel.setDepartment(dto.getDepartment());
                    personnel.setTeam(dto.getTeam());
                    personnel.setStartDate(dto.getStartDate());
                    personnel.setEndDate(dto.getEndDate());
                    personnel.setUnitPrice(dto.getUnitPrice());
                    personnel.setPriceType(dto.getPriceType());
                    personnel.setStatus(dto.getStatus());
                    personnel.setRemark(dto.getRemark());
                    personnel.setCreateTime(java.time.LocalDateTime.now());
                    personnel.setUpdateTime(java.time.LocalDateTime.now());
                    
                    return personnel;
                })
                .collect(Collectors.toList());
        
        return contractPersonnelService.batchCreateContractPersonnel(doList);
    }
    
    @Operation(summary = "更新合同人员", description = "更新合同人员信息")
    @PutMapping("/{id}")
    public Result<ContractPersonnelDO> updateContractPersonnel(
            @Parameter(description = "合同人员ID", required = true, example = "1")
            @PathVariable Long id,
            @Parameter(description = "合同人员信息", required = true)
            @RequestBody ContractPersonnelDO contractPersonnel) {
        log.info("更新合同人员，ID：{}", id);
        contractPersonnel.setId(id);
        return contractPersonnelService.updateContractPersonnel(contractPersonnel);
    }
    
    @Operation(summary = "删除合同人员", description = "删除合同人员记录")
    @DeleteMapping("/{id}")
    public Result<Void> deleteContractPersonnel(
            @Parameter(description = "合同人员ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("删除合同人员，ID：{}", id);
        return contractPersonnelService.deleteContractPersonnel(id);
    }
    
    @Operation(summary = "获取合同人员详情", description = "根据ID获取合同人员详细信息")
    @GetMapping("/{id}")
    public Result<ContractPersonnelDO> getContractPersonnelById(
            @Parameter(description = "合同人员ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("获取合同人员详情，ID：{}", id);
        try {
            ContractPersonnelDO contractPersonnel = contractPersonnelService.getById(id);
            if (contractPersonnel != null) {
                return Result.success(contractPersonnel);
            } else {
                return Result.error(404, "合同人员不存在");
            }
        } catch (Exception e) {
            log.error("获取合同人员详情失败", e);
            return Result.error(500, "获取合同人员详情失败：" + e.getMessage());
        }
    }
}